package com.biz.primus.ms.member.api;

import com.biz.primus.common.utils.DateUtil;
import com.biz.primus.common.utils.StringUtil;
import com.biz.primus.model.member.vo.MemberAddressVo;
import com.biz.primus.model.member.vo.MemberVo;
import com.biz.primus.model.member.vo.request.MemberAddressNewRequestVo;
import com.biz.primus.model.member.vo.request.MemberRegisterRequestVo;
import com.biz.primus.model.member.vo.response.MemberRegisterResponseVo;
import com.biz.primus.ms.base.api.BaseApiController;
import com.biz.primus.ms.member.dao.ro.MemberRo;
import com.biz.primus.ms.member.service.*;
import com.biz.primus.ms.member.trans.MemberRoToMemberVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * 会员服务提供给客服中心的接口
 * @author liujun
 * @date 2017/11/18
 */
@RestController
@RequestMapping("soa/memberService/")
@Api("会员相关接口文档")
public class MemberForCustomerCenterApiController extends BaseApiController {

    private final MemberService memberService;

    private final MemberFavouriteService memberFavouriteService;
    private final MemberFootprintService memberFootprintService;
    private final MemberBankCardService memberBankCardService;
    private final MemberInvoiceService memberInvoiceService;
    private final MemberAuthTokenService memberAuthTokenService;
    private final MemberAddressService memberAddressService;
    private final NoticeService noticeService;

    @Autowired
    public MemberForCustomerCenterApiController(MemberService memberService, MemberFavouriteService memberFavouriteService,
                                                MemberFootprintService memberFootprintService, MemberBankCardService memberBankCardService,
                                                MemberInvoiceService memberInvoiceService, MemberAuthTokenService memberAuthTokenService,
                                                MemberAddressService memberAddressService, NoticeService noticeService)
    {
        this.memberService = memberService;
        this.memberFavouriteService = memberFavouriteService;
        this.memberFootprintService = memberFootprintService;
        this.memberBankCardService = memberBankCardService;
        this.memberInvoiceService = memberInvoiceService;
        this.memberAuthTokenService = memberAuthTokenService;
        this.memberAddressService = memberAddressService;
        this.noticeService = noticeService;
    }

    /**
     * 根据手机号码获取会员vo信息
     * @param mobile
     * @return
     */
    @PostMapping("findMemberVoByMobile")
    public MemberVo findMemberVoByMobile(@RequestParam("mobile") String mobile)
    {
        MemberVo vo = null;
        if (StringUtil.isMobileValid(mobile))
        {
            MemberRo ro = memberService.findRoByMobile(mobile);
            if(ro != null && ro.getId() != null && ro.getId() != 0L)
            {
                vo = new MemberRoToMemberVo().apply(ro);
                vo.setId(ro.getId());
            }
        }
        return vo;
    }

    @ApiOperation("客服中心手机注册")
    @PostMapping("registerByMobile")
    public Boolean registerByMobile(@RequestParam("mobile") String mobile)
    {
        return memberService.registerByMobile(mobile);
    }

    @PostMapping("getAllAddress")
    public List<MemberAddressVo> getAllAddress(@RequestParam("memberId") Long memberId)
    {
        return memberAddressService.getAllAddress(memberId);
    }

    @ApiOperation("保存或更新收货地址")
    @PostMapping("addMemberAddessInfo")
    public Boolean addMemberAddessInfo(@RequestBody MemberAddressVo reqVo)
    {
        try {
            memberAddressService.saveOrUpdateAddress(reqVo);
        }catch (Exception e){
            return false;
        }
        return true;
    }

}


